package com.ztewatch.wearcontacts;

import java.util.ArrayList;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.ztewatch.wearcontacts.calllog.CallLogQuery;
import com.ztewatch.wearcontacts.calllog.RejectedCallLogAdapter;

public class WearContactRejectedCallActivity extends Activity {
	private ListView mListView;
	private RejectedCallLogAdapter mCallLogAdapter;
	private TextView m_empty_view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wear_contact_rejected_call);
		initview();
	}

	private void initview() {
		mListView = (ListView) findViewById(R.id.rejectedcall_log_list);
		mCallLogAdapter = new RejectedCallLogAdapter(this);
		mListView.setAdapter(mCallLogAdapter);
		// mListView.setOnItemClickListener(this);
		m_empty_view = (TextView) findViewById(R.id.rejectedcall_log_empty_view);
	}

	private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

		@Override
		public void onLoaderReset(Loader<Cursor> arg0) {

		}

		@Override
		public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
			mCallLogAdapter.changeCursor(c);
			if (c.getCount() == 0) {
				m_empty_view.setVisibility(View.VISIBLE);
			} else {
				m_empty_view.setVisibility(View.GONE);
			}
		}

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			CursorLoader cursorLoader = new CursorLoader(
					WearContactRejectedCallActivity.this);
			configCursorLoader(cursorLoader, Calls.MISSED_TYPE, false, -1);
			return cursorLoader;
		}
	};

	private void configCursorLoader(CursorLoader cursorLoader, int callType,
			boolean newOnly, long newerThan) {
		Uri uri = Calls.CONTENT_URI
				.buildUpon()
				.appendQueryParameter(Calls.LIMIT_PARAM_KEY,
						Integer.toString(1000)).build();

		StringBuilder where = new StringBuilder();
		ArrayList<String> selectionArgs = new ArrayList<String>();

		if (newOnly) {
			where.append(Calls.NEW);
			where.append(" = 1");
		}

		if (callType > -1) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			// Add a clause to fetch only items of type voicemail.
			where.append(String.format("(%s = ?)", Calls.TYPE));
			// Add a clause to fetch only items newer than the requested date
			selectionArgs.add(Integer.toString(callType));
		}

		if (newerThan > 0) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			where.append(String.format("(%s > ?)", Calls.DATE));
			selectionArgs.add(Long.toString(newerThan));
		}
		if (where.length() > 0) {
			where.append(" AND ");
		}
		where.append(Calls.CACHED_NAME +" is null");

		final String selection = where.length() > 0 ? where.toString() : null;
		cursorLoader.setUri(uri);
		cursorLoader.setSelection(selection);
		cursorLoader.setSelectionArgs(selectionArgs.toArray(new String[0]));
		cursorLoader.setProjection(CallLogQuery._PROJECTION);
		cursorLoader.setSortOrder(Calls.DEFAULT_SORT_ORDER);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getLoaderManager().initLoader(0, null, loaderCallbacks);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getLoaderManager().destroyLoader(0);
	}
}
